import collections
import enum
import threading
# ----------------------------------------
try:
    from .task import Task
except:
    from task import Task
# --------------------------------------------------


class Job:

    def __init__(self, job_id):
        self.id = job_id
        # --------------------------------------------------
        self._tasks = collections.OrderedDict()
        self._taskstatus = collections.OrderedDict()
        # --------------------------------------------------
        self._statuslock = threading.RLock()
        self._taskslock = threading.RLock()

    def add_task(self, task_id, cb_func, args=None, kwargs=None):
        with self._taskslock:
            task = self._tasks.get(task_id)
            if task:
                return task
            # ----------------------------------------
            args = args or ()
            kwargs = kwargs or {}
            # ----------------------------------------
            task = Task(self, task_id, ckr=cb_func, args=args, kwargs=kwargs)
            self._tasks[task_id] = task
        # --------------------------------------------------
        with self._statuslock:
            self._taskstatus[task_id] = False
        # --------------------------------------------------
        return task

    def ls_tasks(self):
        with self._taskslock:
            return self._tasks.keys()

    def select_task(self, task_id):
        with self._tasks:
            return self._tasks.get(task_id)

    def rm_task(self, task_id):
        with self._taskslock:
            # --------------------------------------------------
            if task_id in self._tasks:
                del self._tasks[task_id]


    # def add_task(self, task):
    #     with self._taskslock:
    #         if task.id in self._tasks:
    #             return True
    #         # ----------------------------------------
    #         self._tasks[task.id] = task
